package org.travel.api.travel.controller.wmh.user;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.travel.api.travel.dao.wmh.user.UserDao;
import org.travel.api.travel.pojo.User;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Random;

@RestController
@RequestMapping("/User")
@CrossOrigin("*")
public class user {
    @Autowired
    private UserDao userDao;

    @RequestMapping("/login")
    public Object userLogin(String loginName, String password,String phone)throws Exception{
        User user =userDao.getLogin(loginName,phone);
        if (user.getPhone().equals(phone)){
            return user;
        }
        if (user == null){
            return "";
        }
        if (!user.getPassword().equals(password)){
            return "";
        }
        return user;
    }

    @RequestMapping("/yzm")
    public String yzm(String phone)throws Exception{
        if (phone != "" && phone != null) {
            String testUsername = "jiji";
            String testPassword = "123456789";
            String testPhone = phone;
            String verifyCode = String.valueOf(new Random().nextInt(899999) + 100000);
            String testContent = "请在60s内输入该验证码：" + verifyCode;
            String httpUrl = "http://api.smsbao.com/sms";
            StringBuffer httpArg = new StringBuffer();
            httpArg.append("u=").append(testUsername).append("&");
            httpArg.append("p=").append(md5(testPassword)).append("&");
            httpArg.append("m=").append(testPhone).append("&");
            httpArg.append("c=").append(encodeUrlString(testContent, "UTF-8"));

            String result = request(httpUrl, httpArg.toString());
            if (result.equals("0")) {
                return verifyCode;
            }
        }
        return "";
    }

    public static String request(String httpUrl, String httpArg) {
        BufferedReader reader = null;
        String result = null;
        StringBuffer sbf = new StringBuffer();
        httpUrl = httpUrl + "?" + httpArg;

        try {
            URL url = new URL(httpUrl);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");
            connection.connect();
            InputStream is = connection.getInputStream();
            reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            String strRead = reader.readLine();
            if (strRead != null) {
                sbf.append(strRead);
                while ((strRead = reader.readLine()) != null) {
                    sbf.append("\n");
                    sbf.append(strRead);
                }
            }
            reader.close();
            result = sbf.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public static String md5(String plainText) {
        StringBuffer buf = null;
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(plainText.getBytes());
            byte b[] = md.digest();
            int i;
            buf = new StringBuffer("");
            for (int offset = 0; offset < b.length; offset++) {
                i = b[offset];
                if (i < 0)
                    i += 256;
                if (i < 16)
                    buf.append("0");
                buf.append(Integer.toHexString(i));
            }
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return buf.toString();
    }

    public static String encodeUrlString(String str, String charset) {
        String strret = null;
        if (str == null)
            return str;
        try {
            strret = java.net.URLEncoder.encode(str, charset);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return strret;
    }
}
